﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;

namespace Model
{
  public class UIBags : UIInventory
  {
    /// <summary>
    /// 装备按钮
    /// </summary>
    public Button EquipButton;
    public Button AllButton;
    public Button WeaponButton;
    public Button MaterialButton;
    protected override void Awake()
    {
      base.Awake();

      this.EquipButton.onClick.AddListener(() => { this.ShowType(ItemType.Equipment); });

      this.WeaponButton.onClick.AddListener(() => { this.ShowType(ItemType.Weapon); });

      this.MaterialButton.onClick.AddListener(() => { this.ShowType(ItemType.Material); });

      this.AllButton.onClick.AddListener(ShowAll);
    }

    private void ShowType(ItemType itemType)
    {
      foreach (UISlot slot in this.slots)
      {
        if (slot.IsEmpty)
        {
          slot.gameObject.SetActive(false);
          continue;
        }

        if (slot.VM.Item.ItemType != itemType)
          slot.gameObject.SetActive(false);
        else
          slot.gameObject.SetActive(true);
      }
    }

    private void ShowAll()
    {
      foreach (UISlot slot in this.slots)
      {
        slot.gameObject.SetActive(true);
      }
    }

    public void Update()
    {
      if (Input.GetKeyDown(KeyCode.A))
      {
        List<ItemEntity> list = Game.InventoryManager.Dictionary.Values.ToList();

        System.Random random = new System.Random();
        int index = random.Next(0, list.Count);

        this.ItemAdd(new ItemViewModel
        {
          Item = list[index],
          Count = 1
        });
      }
    }
  }
}